/*
 * 著作权声明
 * Copyright (c) 2025.8 拾壹
 * 本代码为个人独立开发，未经本人书面许可，任何组织或个人不得擅自复制、修改、分发、用于商业用途。
 * 联系方式：sjw123521@qq.com
 */
 package com.xmqsfsky1017.bzgzs;

import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Spinner;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
	
    Room.Builder builder = null;
    Room renter = null;
	
	Spinner roomSpinner = null;
    EditText etRenter = null;
    EditText etWaterUnitPrice = null;
    EditText etZilaUnitPrice = null;
    EditText etLastWater = null;
    EditText etThisWater = null;
    EditText etLastZila = null;
    EditText etThisZila = null;
    EditText etRent = null;
    Button btnReceiptWriting = null;
	Button btnClear=null;
	Button btnAbout=null;
	TextView tvReceipt=null;
	EditText etWriter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        roomSpinner = findViewById(R.id.room_spinner);
        etRenter = findViewById(R.id.renter_edittext);
        etWaterUnitPrice = findViewById(R.id.water_unit_price);
        etZilaUnitPrice = findViewById(R.id.zila_unit_price);
        etLastWater = findViewById(R.id.last_water);
        etThisWater = findViewById(R.id.this_water);
        etLastZila = findViewById(R.id.last_zila);
        etThisZila = findViewById(R.id.this_zila);
        etRent = findViewById(R.id.rent);
        btnReceiptWriting = findViewById(R.id.receipt_writing);
		btnClear=findViewById(R.id.clear);
	    tvReceipt=findViewById(R.id.receipt);
		etWriter=findViewById(R.id.writer);
		btnAbout=findViewById(R.id.about);

        String[] rooms = {"301", "302", "401", "402"};
        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, rooms);
        roomSpinner.setAdapter(adapter);
		
		EditText[] numEtArray = {etWaterUnitPrice,etZilaUnitPrice,etLastWater,
			etThisWater,etLastZila,etThisZila,etRent};

        etWaterUnitPrice.setText(getString(R.string.water_unit_price));
        etZilaUnitPrice.setText(getString(R.string.zila_unit_price));
        etWriter.setText(getString(R.string.default_writer));
		
        btnReceiptWriting.setOnClickListener(this::receiptWritingToDo);
		btnClear.setOnClickListener(this::editTextClear);
		btnAbout.setOnClickListener(this::aboutToDo);
		etRenter.setOnFocusChangeListener(this::checkNameToDo);
		etWriter.setOnFocusChangeListener(this::checkNameToDo);
		
		for (EditText et:numEtArray) {
			et.setOnFocusChangeListener(this::checkIsMoney);
		}
		
    }

    private void showErrorDialog() {
        new AlertDialog.Builder(this)
                .setTitle(this.getString(R.string.error_title))
                .setMessage(this.getString(R.string.error_invalid_input))
                .setCancelable(false)
                .setPositiveButton(
				    this.getString(R.string.dialog_ok), 
					(dialog, which) -> dialog.dismiss()
				)
                .show();
    }
	
	private void editTextClear(View v) {
		etRenter.setText("");
        etLastWater.setText("");
        etThisWater.setText("");
        etLastZila.setText("");
        etThisZila.setText("");
        etRent.setText("");
	}
	
	private void  informationNotCompletedAlert() {
		new AlertDialog.Builder(this)
                        .setTitle(this.getString(R.string.error_title))
                        .setMessage(this.getString(R.string.error_no_tenant))
                        .setPositiveButton(this.getString(R.string.dialog_ok), null)
                        .setCancelable(false)
                        .show();
	}
	
	private void receiptWritingToDo (View view) {
		try{
			String renterName=etRenter.getText().toString();
			String roomNumber=(String)roomSpinner.getSelectedItem();
			String writerName=etWriter.getText().toString();
			if (roomNumber == null) {
                showErrorDialog();
                return;
            }
			builder=new Room.Builder(this,renterName,roomNumber,writerName);
			builder.setWaterUnitPrice(new BaauzougungNumber(etWaterUnitPrice.getText().toString()))
				   .setZilaUnitPrice(new BaauzougungNumber(etZilaUnitPrice.getText().toString()))
				   .setLastWater(new BaauzougungNumber(etLastWater.getText().toString()))
				   .setLastZila(new BaauzougungNumber(etLastZila.getText().toString()))
				   .setRent(new BaauzougungNumber(etRent.getText().toString()))
				   .setThisWater(new BaauzougungNumber(etThisWater.getText().toString()))
				   .setThisZila(new BaauzougungNumber(etThisZila.getText().toString()));
		    renter=builder.build();
			
		} catch (IllegalStateException e) {
			informationNotCompletedAlert();
		}catch  (IllegalArgumentException e) {
			showErrorDialog();
		}
        try {
            String receipt = renter.getReceipt(this);
	        tvReceipt.setText(receipt);
            Toast.makeText(this, "收据已生成", Toast.LENGTH_SHORT).show();
            renter = null;
			builder=null;
        } catch (Exception e) {
            showErrorDialog();
        }
    }
	
	private void checkIsMoney(View v,boolean hasFocus) {
		if(hasFocus)return;
		EditText et=(EditText)v;
		String text=et.getText().toString().trim();
		if (text.isEmpty()||!text.matches(("^\\d+(\\.\\d{1,2})?$"))){
			showErrorDialog();
			et.setText("");
		}
	}
	
	private void checkNameToDo(View v,boolean hasFocus) {
		if(hasFocus)return;
		EditText et=(EditText)v;
		String text=et.getText().toString().trim();
		if (text.isEmpty()) {
			showErrorDialog();
			return;
		}
	}
	
	private void aboutToDo(View v) {
		Intent intent=new Intent(this,AboutActivity.class);
		startActivity(intent);
	}
}
